package com.micromsg.basic.sys.controller;

import com.micromsg.basic.msg.vo.AllFriendVO;
import com.micromsg.basic.sys.service.UserService;
import com.micromsg.basic.sys.vo.entity.UserEntityVO;
import com.micromsg.basic.sys.vo.request.UserRequestVO;
import com.micromsg.basic.sys.vo.response.UserResponseVO;
import com.micromsg.common.core.ApiResult;
import com.micromsg.common.core.PageResult;
import com.micromsg.common.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表
 *
 * @author yangyang
 * @date 2024/04/06
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1/sys")
@Api(description = "用户表")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取用户表分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<PageResult<UserResponseVO>>> getUserPageList(UserRequestVO requestVO) {
        try {
            PageResult<UserResponseVO> pageResult = this.userService.queryUserPageList(requestVO);
            return ApiResult.ok(pageResult);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("获取用户表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/user/allfriend/{userId}", method = RequestMethod.GET)
    public AllFriendVO getAllFriend(@PathVariable("userId") String userId) {
        AllFriendVO allFriend = this.userService.getAllFriend(userId);
        return allFriend;
    }

    @ApiOperation("获取用户表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<UserEntityVO>> getUser(@PathVariable("id") String id) {
        try {
            UserEntityVO userEntityVO = this.userService.getUserEntityVO(id);
            return ApiResult.ok(userEntityVO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("新增用户表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> addUser(@RequestBody @Validated UserEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.userService.addUser(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("更新用户表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public ResponseEntity<ApiResult<Object>> updateUser(@RequestBody @Validated UserEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.userService.updateUser(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }


    @ApiOperation("删除用户表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ApiResult<Object>> deleteUser(@PathVariable("id") String id) {
        try {
            this.userService.deleteUser(id);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }
}
